<template>
  <transition>
    <div class="car-item" v-if="selfItem === curItem">
      <slot></slot>
    </div>
  </transition>
</template>

<script>
import {getCurrentInstance, reactive, toRefs, watch} from "vue";

export default {
  name: "KCarouselItem",
  setup() {
    const instance = getCurrentInstance()
    const state = reactive({
      selfItem: instance.vnode.key,
      curItem: instance.parent.ctx.curItem
    })

    watch(() => {
      return instance.parent.ctx.curItem
    }, (value) => {
      state.curItem = value
    })

    return {
      ...toRefs(state)
    }
  }


}
</script>

<style>

.car-item {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

img {
  width: 100%;
}

.v-enter-active,
.v-leave-active {
  transition: all .5s linear;
}

.v-enter-active {
  transform: translateX(100%);
}

.v-enter-to {
  transform: translateX(0);
}

.v-leave-active {
  transform: translateX(0);
}

.v-leave-to {
  transform: translateX(-100%);
}


</style>